Skip to content

perf(wilcoxon): compute CLES via searchsorted in O(n log n) - #572

Open
soodoku wants to merge 1 commit into
raphaelvallat:mainfrom
finite-sample:perf/wilcoxon-cles-searchsorted
Open

soodoku wants to merge 1 commit into
raphaelvallat:mainfrom
finite-sample:perf/wilcoxon-cles-searchsorted

Conversation

@soodoku

@soodoku soodoku commented Nov 24, 2026 •

Copy link
Copy Markdown

Summary

This PR speeds up pingouin.wilcoxon by computing the Common Language Effect Size (CLES) via np.searchsorted on sorted y in $O(n \log n)$ time and $O(n)$ memory — matching the approach already used in pingouin.compute_effsize(..., eftype="cles") — instead of materializing an (n, n) pairwise difference matrix. Results are numerically identical (0.00e+00 max difference), with 2.2x–26x speedups on moderate-to-large samples.

Impact rating: P2 — Substantial, benchmarked speedup (up to 26x at n = 5000) and $O(n^2) \to O(n)$ memory reduction in a core nonparametric test, with zero behavioral change.

Model used: Gemini 4 Neon via Antigravity CLI

Benchmarks

Median wall time over repeated runs (integers in [0, 100) with ties, method="asymptotic"):

Workload Before After Speedup
n = 500 (15 runs) 0.65 ms 0.30 ms 2.2x
n = 5000 (5 runs) 32.14 ms 1.23 ms 26.1x

At n = 5000, this also avoids allocating the 200 MB temporary (5000, 5000) float64 difference matrix.

Benchmark code:

import time
import numpy as np
import pingouin as pg

rng = np.random.default_rng(42)
for n, repeats in [(500, 15), (5000, 5)]:
    x = rng.integers(0, 100, size=n)
    y = rng.integers(0, 100, size=n)
    pg.wilcoxon(x, y, method="asymptotic")  # warmup
    times = []
    for _ in range(repeats):
        t0 = time.perf_counter()
        pg.wilcoxon(x, y, method="asymptotic")
        times.append((time.perf_counter() - t0) * 1e3)
    print(f"n={n}: median={np.median(times):.2f} ms")

Equivalence

Sorting y once and counting strictly smaller and tied entries for each x via np.searchsorted(..., side="left") and np.searchsorted(..., side="right") preserves the exact Vargha–Delaney CLES formula and the 1 - cles inversion when alternative == "less".

Checked against the brute-force np.where(diff == 0, 0.5, diff > 0).mean() reference across 50 randomized inputs with heavy ties for all three alternative values ("two-sided", "greater", "less"):

  • Max absolute difference in CLES: 0.00e+00

Verification

  • Added regression tests covering ties across all three alternative values in tests/test_nonparametric.py.
  • ruff check and ruff format --check pass cleanly.
  • Full test suite (pytest -q): 171 passed.

Checklist

  • The impact rating is stated and justified (P1-P3)
  • An issue was opened or discussed first (required for P3 and new features) (opened before seeing Guidelines for AI-assisted pull requests #558)
  • The branch is up to date with main, and I checked open PRs for conflicts
  • The PR was opened as a draft (opened before seeing Guidelines for AI-assisted pull requests #558)
  • An AI review was run, the issues were fixed, and a summary was posted
  • /simplify was run (preferred, Claude Code) (used Antigravity CLI rather than Claude Code; diff was reviewed for simplicity)
  • The description starts with my own summary
  • The exact model used is reported
  • Only necessary tests were added
  • The changelog was not updated (I typically update it once before a new release)
  • The PR is small and focused, or stacked

@codecov

codecov Bot commented Nov 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.93%. Comparing base (3d4db65) to head (53829fd).

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #572   +/-   ##
=======================================
  Coverage   99.93%   99.93%           
=======================================
  Files          19       19           
  Lines        3121     3123    +2     
  Branches      586      586           
=======================================
+ Hits         3119     3121    +2     
  Misses          1        1           
  Partials        1        1           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@soodoku

soodoku commented Nov 25, 2026

Copy link
Copy Markdown
Author

AI Review Summary

  • What was checked: Diff in src/pingouin/nonparametric.py and tests/test_nonparametric.py, equivalence of np.searchsorted(..., side="left"/"right") against the pairwise difference matrix formula across ties and all three alternative values, consistency with compute_effsize(..., eftype="cles"), y is None branch preservation, benchmark methodology, test scope, and merge-conflict check against main and open PRs (non-overlapping hunk from perf(mwu): compute CLES in O(1) from Mann-Whitney U statistic #571).
  • Issues found: None. The change is compact (4 lines) and avoids the $O(n^2)$ temporary allocation.
  • CI status: All 16 checks passing (Ruff, docs, full PyTest OS/dependency matrix, 100% patch coverage on Codecov).

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant